package gui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.Serializable;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

import backproped.NeuralNetwork;

public class GUI extends JFrame implements ActionListener, Serializable {

	private static final long serialVersionUID = 1L;

	private static final int FRAME_WIDTH = 900;
	private static final int FRAME_HEIGHT = 650;

	public static final Font VERDANA = new Font("Verdana", 0, 10);
	
	private String defaultDir = null;

	// The Neural Net that will do the work
	NeuralNetwork neuralNet;

	private JMenuBar MenuBar;

	private JTabbedPane tabbedPane;
	private JPanel tabTraining;
	private JLabel lblTraining;
	private JButton btnSaveGraphic;
	private JButton btnLoadGraphic;
	private JButton btnClearGraphic;
	private JButton btnClassifyInput;
	private JButton btnLoadWeights;
	private JButton btnSaveWeights;
	private JButton btnRandomNet;
	private JButton btnTrainTillValid;
	private JButton btnAddRandomNoise;
	
	private JButton btnViewTrainingSamples;
	private SimplePaint sp;
	private JTextField txtDesired;
	private JLabel lblDesired;

	private JPanel panOutputNeurons;
	private JLabel lblOutputNeuron0;
	private JLabel lblOutputNeuron1;
	private JLabel lblOutputNeuron2;
	private JLabel lblOutputNeuron3;
	private JLabel lblOutputNeuron4;
	private JLabel lblOutputNeuron5;
	private JLabel lblOutputNeuron6;
	private JLabel lblOutputNeuron7;
	private JLabel lblOutputNeuron8;
	private JLabel lblOutputNeuron9;
	private JTextField txtOutputNeuron0;
	private JTextField txtOutputNeuron1;
	private JTextField txtOutputNeuron2;
	private JTextField txtOutputNeuron3;
	private JTextField txtOutputNeuron4;
	private JTextField txtOutputNeuron5;
	private JTextField txtOutputNeuron6;
	private JTextField txtOutputNeuron7;
	private JTextField txtOutputNeuron8;
	private JTextField txtOutputNeuron9;

	private JLabel lblOutputClass;
	private JTextField txtOutputClass;

	private JLabel lblStatus;

	public static Font getVerdana() {// Returns default font for this
										// application.
		return VERDANA;
	}

	public GUI() {
		try {// Sets the theme to the operating system.
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		// setDefaultCloseOperation(EXIT_ON_CLOSE);

		Container contentPane = getContentPane();

		// Set the frame properties:
		setTitle("Neural Network : Digit Recognition");
		setIconImage(null);
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
		
		// setResizable(false);

		contentPane.setBackground(Color.WHITE);
		contentPane.setLayout(null);

		contentPane.setLayout(null);

		// Create tabbedPane.
		tabbedPane = new JTabbedPane();

		// Create tabNowPlaying and its components.
		tabTraining = new JPanel();
		tabTraining.setLayout(null);
		tabTraining.setOpaque(false);

		lblTraining = new JLabel("Training Image");
		lblTraining.setBounds(10, 15, 370, 15);
		lblTraining.setForeground(Color.BLACK);
		lblTraining.setOpaque(false);
		lblTraining.setFont(getVerdana());
		tabTraining.add(lblTraining);

		lblDesired = new JLabel("Desired Classification");
		lblDesired.setBounds(345, 0, 125, 25);
		lblDesired.setForeground(Color.BLACK);
		lblDesired.setOpaque(false);
		lblDesired.setFont(getVerdana());
		tabTraining.add(lblDesired);

		txtDesired = new JTextField();
		txtDesired.setBounds(345, 20, 125, 25);
		txtDesired.setForeground(Color.BLACK);
		txtDesired.setOpaque(false);
		txtDesired.setFont(getVerdana());
		txtDesired.setText("2");
		tabTraining.add(txtDesired);

		sp = new SimplePaint();
		sp.setSize(320, 400); // same size as defined in the HTML APPLET
		sp.setBackground(Color.WHITE);
		tabTraining.add(sp);

		sp.init();

		btnSaveGraphic = new JButton("Save Image");
		btnSaveGraphic.setBounds(120, 405, 120, 20);
		btnSaveGraphic.setForeground(Color.BLACK);
		btnSaveGraphic.setFont(getVerdana());
		btnSaveGraphic.setVisible(true);
		btnSaveGraphic.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnSaveGraphic.addActionListener(this);
		btnSaveGraphic.setEnabled(true);
		tabTraining.add(btnSaveGraphic);

		btnLoadGraphic = new JButton("Load Image");
		btnLoadGraphic.setBounds(0, 405, 120, 20);
		btnLoadGraphic.setForeground(Color.BLACK);
		btnLoadGraphic.setFont(getVerdana());
		btnLoadGraphic.setVisible(true);
		btnLoadGraphic.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnLoadGraphic.addActionListener(this);
		btnLoadGraphic.setEnabled(true);
		tabTraining.add(btnLoadGraphic);

		btnClearGraphic = new JButton("Clear Image");
		btnClearGraphic.setBounds(240,405,120,20);
		btnClearGraphic.setForeground(Color.BLACK);
		btnClearGraphic.setFont(getVerdana());
		btnClearGraphic.setVisible(true);
		btnClearGraphic.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnClearGraphic.addActionListener(this);
		btnClearGraphic.setEnabled(true);
		tabTraining.add(btnClearGraphic);		
		
		
		btnClassifyInput = new JButton("Classify Input");
		btnClassifyInput.setBounds(360, 405, 120, 20);
		btnClassifyInput.setForeground(Color.BLACK);
		btnClassifyInput.setFont(getVerdana());
		btnClassifyInput.setVisible(true);
		btnClassifyInput.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnClassifyInput.addActionListener(this);
		btnClassifyInput.setEnabled(true);
		tabTraining.add(btnClassifyInput);

		btnLoadWeights = new JButton("Load Weights");
		btnLoadWeights.setBounds(0, 425, 120, 20);
		btnLoadWeights.setForeground(Color.BLACK);
		btnLoadWeights.setFont(getVerdana());
		btnLoadWeights.setVisible(true);
		btnLoadWeights.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnLoadWeights.addActionListener(this);
		btnLoadWeights.setEnabled(true);
		tabTraining.add(btnLoadWeights);

		btnSaveWeights = new JButton("Save Weights");
		btnSaveWeights.setBounds(120, 425, 120, 20);
		btnSaveWeights.setForeground(Color.BLACK);
		btnSaveWeights.setFont(getVerdana());
		btnSaveWeights.setVisible(true);
		btnSaveWeights.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnSaveWeights.addActionListener(this);
		btnSaveWeights.setEnabled(true);
		tabTraining.add(btnSaveWeights);

		btnRandomNet = new JButton("Load Random Net");
		btnRandomNet.setBounds(240, 425, 120, 20);
		btnRandomNet.setForeground(Color.BLACK);
		btnRandomNet.setFont(getVerdana());
		btnRandomNet.setVisible(true);
		btnRandomNet.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnRandomNet.addActionListener(this);
		btnRandomNet.setEnabled(true);
		tabTraining.add(btnRandomNet);

		btnTrainTillValid = new JButton("Train Net");
		btnTrainTillValid.setBounds(360, 425, 120, 20);
		btnTrainTillValid.setForeground(Color.BLACK);
		btnTrainTillValid.setFont(getVerdana());
		btnTrainTillValid.setVisible(true);
		btnTrainTillValid.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnTrainTillValid.addActionListener(this);
		btnTrainTillValid.setEnabled(true);
		tabTraining.add(btnTrainTillValid);
		
		btnAddRandomNoise = new JButton("Add Noise");
		btnAddRandomNoise.setBounds(0,445,120,20);
		btnAddRandomNoise.setForeground(Color.BLACK);
		btnAddRandomNoise.setFont(getVerdana());
		btnAddRandomNoise.setVisible(true);
		btnAddRandomNoise.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnAddRandomNoise.addActionListener(this);
		btnAddRandomNoise.setEnabled(true);
		tabTraining.add(btnAddRandomNoise);
		
		btnViewTrainingSamples = new JButton("Show Training Set");
		btnViewTrainingSamples.setBounds(120,445,120,20);
		btnViewTrainingSamples.setForeground(Color.BLACK);
		btnViewTrainingSamples.setFont(getVerdana());
		btnViewTrainingSamples.setVisible(true);
		btnViewTrainingSamples.setVerticalAlignment((int) TOP_ALIGNMENT);
		btnViewTrainingSamples.addActionListener(this);
		btnViewTrainingSamples.setEnabled(true);
		tabTraining.add(btnViewTrainingSamples);
		
		tabbedPane.add("Train", tabTraining);

		
		
		tabbedPane.setBounds(0, 80, 500, 500);
		tabbedPane.setBackground(Color.decode("#ffffff"));
		tabbedPane.setForeground(Color.BLACK);
		tabbedPane.setFont(getVerdana());
		// tabbedPane.
		contentPane.add(tabbedPane);

		// 
		// Output Neuron Output
		//

		panOutputNeurons = new JPanel();
		panOutputNeurons.setLayout(null);
		panOutputNeurons.setOpaque(false);
		// panOutputNeurons.setBackground(Color.RED);
		panOutputNeurons.setBounds(501, 90, 500, 456);
		panOutputNeurons.setVisible(true);

		lblOutputNeuron0 = new JLabel("Output Neuron 0: ");
		lblOutputNeuron0.setBounds(0, 0, 125, 15);
		lblOutputNeuron0.setForeground(Color.BLACK);
		lblOutputNeuron0.setOpaque(false);
		lblOutputNeuron0.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron0);

		lblOutputNeuron1 = new JLabel("Output Neuron 1: ");
		lblOutputNeuron1.setBounds(0, 20, 125, 15);
		lblOutputNeuron1.setForeground(Color.BLACK);
		lblOutputNeuron1.setOpaque(false);
		lblOutputNeuron1.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron1);

		lblOutputNeuron2 = new JLabel("Output Neuron 2: ");
		lblOutputNeuron2.setBounds(0, 40, 125, 15);
		lblOutputNeuron2.setForeground(Color.BLACK);
		lblOutputNeuron2.setOpaque(false);
		lblOutputNeuron2.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron2);

		lblOutputNeuron3 = new JLabel("Output Neuron 3: ");
		lblOutputNeuron3.setBounds(0, 60, 125, 15);
		lblOutputNeuron3.setForeground(Color.BLACK);
		lblOutputNeuron3.setOpaque(false);
		lblOutputNeuron3.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron3);

		lblOutputNeuron4 = new JLabel("Output Neuron 4: ");
		lblOutputNeuron4.setBounds(0, 80, 125, 15);
		lblOutputNeuron4.setForeground(Color.BLACK);
		lblOutputNeuron4.setOpaque(false);
		lblOutputNeuron4.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron4);

		lblOutputNeuron5 = new JLabel("Output Neuron 5: ");
		lblOutputNeuron5.setBounds(0, 100, 125, 15);
		lblOutputNeuron5.setForeground(Color.BLACK);
		lblOutputNeuron5.setOpaque(false);
		lblOutputNeuron5.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron5);

		lblOutputNeuron6 = new JLabel("Output Neuron 6: ");
		lblOutputNeuron6.setBounds(0, 120, 125, 15);
		lblOutputNeuron6.setForeground(Color.BLACK);
		lblOutputNeuron6.setOpaque(false);
		lblOutputNeuron6.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron6);

		lblOutputNeuron7 = new JLabel("Output Neuron 7: ");
		lblOutputNeuron7.setBounds(0, 140, 125, 15);
		lblOutputNeuron7.setForeground(Color.BLACK);
		lblOutputNeuron7.setOpaque(false);
		lblOutputNeuron7.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron7);

		lblOutputNeuron8 = new JLabel("Output Neuron 8: ");
		lblOutputNeuron8.setBounds(0, 160, 125, 15);
		lblOutputNeuron8.setForeground(Color.BLACK);
		lblOutputNeuron8.setOpaque(false);
		lblOutputNeuron8.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron8);

		lblOutputNeuron9 = new JLabel("Output Neuron 9: ");
		lblOutputNeuron9.setBounds(0, 180, 125, 15);
		lblOutputNeuron9.setForeground(Color.BLACK);
		lblOutputNeuron9.setOpaque(false);
		lblOutputNeuron9.setFont(getVerdana());
		panOutputNeurons.add(lblOutputNeuron9);

		// -------------------------------

		int outputNeuronWidth_txt = 50;
		int outputNeuronLeft_txt = 110;
		txtOutputNeuron0 = new JTextField();
		txtOutputNeuron0.setBounds(outputNeuronLeft_txt, 0,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron0.setForeground(Color.BLACK);
		txtOutputNeuron0.setOpaque(false);
		txtOutputNeuron0.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron0);

		txtOutputNeuron1 = new JTextField();
		txtOutputNeuron1.setBounds(outputNeuronLeft_txt, 20,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron1.setForeground(Color.BLACK);
		txtOutputNeuron1.setOpaque(false);
		txtOutputNeuron1.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron1);

		txtOutputNeuron2 = new JTextField();
		txtOutputNeuron2.setBounds(outputNeuronLeft_txt, 40,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron2.setForeground(Color.BLACK);
		txtOutputNeuron2.setOpaque(false);
		txtOutputNeuron2.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron2);

		txtOutputNeuron3 = new JTextField();
		txtOutputNeuron3.setBounds(outputNeuronLeft_txt, 60,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron3.setForeground(Color.BLACK);
		txtOutputNeuron3.setOpaque(false);
		txtOutputNeuron3.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron3);

		txtOutputNeuron4 = new JTextField();
		txtOutputNeuron4.setBounds(outputNeuronLeft_txt, 80,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron4.setForeground(Color.BLACK);
		txtOutputNeuron4.setOpaque(false);
		txtOutputNeuron4.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron4);

		txtOutputNeuron5 = new JTextField();
		txtOutputNeuron5.setBounds(outputNeuronLeft_txt, 100,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron5.setForeground(Color.BLACK);
		txtOutputNeuron5.setOpaque(false);
		txtOutputNeuron5.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron5);

		txtOutputNeuron6 = new JTextField();
		txtOutputNeuron6.setBounds(outputNeuronLeft_txt, 120,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron6.setForeground(Color.BLACK);
		txtOutputNeuron6.setOpaque(false);
		txtOutputNeuron6.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron6);

		txtOutputNeuron7 = new JTextField();
		txtOutputNeuron7.setBounds(outputNeuronLeft_txt, 140,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron7.setForeground(Color.BLACK);
		txtOutputNeuron7.setOpaque(false);
		txtOutputNeuron7.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron7);

		txtOutputNeuron8 = new JTextField();
		txtOutputNeuron8.setBounds(outputNeuronLeft_txt, 160,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron8.setForeground(Color.BLACK);
		txtOutputNeuron8.setOpaque(false);
		txtOutputNeuron8.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron8);

		txtOutputNeuron9 = new JTextField();
		txtOutputNeuron9.setBounds(outputNeuronLeft_txt, 180,
				outputNeuronWidth_txt, 15);
		txtOutputNeuron9.setForeground(Color.BLACK);
		txtOutputNeuron9.setOpaque(false);
		txtOutputNeuron9.setFont(getVerdana());
		panOutputNeurons.add(txtOutputNeuron9);

		lblOutputClass = new JLabel("Output Classification");
		lblOutputClass.setBounds(outputNeuronLeft_txt + outputNeuronWidth_txt
				+ 20, 80, 120, 15);
		lblOutputClass.setForeground(Color.BLACK);
		lblOutputClass.setOpaque(false);
		lblOutputClass.setFont(getVerdana());
		panOutputNeurons.add(lblOutputClass);

		txtOutputClass = new JTextField();
		txtOutputClass.setBounds(outputNeuronLeft_txt + outputNeuronWidth_txt
				+ 20, 100, 120, 40);
		txtOutputClass.setForeground(Color.BLACK);
		txtOutputClass.setOpaque(false);
		txtOutputClass.setFont(getVerdana());
		panOutputNeurons.add(txtOutputClass);

		contentPane.add(panOutputNeurons);

		lblStatus = new JLabel("");
		lblStatus.setBounds(0, 590, 900, 25);
		lblStatus.setForeground(Color.BLACK);
		lblStatus.setBackground(new Color(203, 203, 203));
		lblStatus.setOpaque(true);
		lblStatus.setFont(getVerdana());

		contentPane.add(lblStatus);

		setSize(FRAME_WIDTH, FRAME_HEIGHT);
		setVisible(true);

		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.exit(0);
			};
		});

		neuralNet = new NeuralNetwork();
	}

	public void setStatus(String status) {
		lblStatus.setText(status);
	}

	public void setNetworkNeuronOutput(double[] networkOutput) {
		for (int i = 0; i < networkOutput.length; i++) {
			if (networkOutput[i] == 1) {
				this.setNetworkClassification(i);
				i = networkOutput.length;
			}
		}
		txtOutputNeuron0.setText(String.valueOf(networkOutput[0]));
		txtOutputNeuron1.setText(String.valueOf(networkOutput[1]));
		txtOutputNeuron2.setText(String.valueOf(networkOutput[2]));
		txtOutputNeuron3.setText(String.valueOf(networkOutput[3]));
		txtOutputNeuron4.setText(String.valueOf(networkOutput[4]));
		txtOutputNeuron5.setText(String.valueOf(networkOutput[5]));
		txtOutputNeuron6.setText(String.valueOf(networkOutput[6]));
		txtOutputNeuron7.setText(String.valueOf(networkOutput[7]));
		txtOutputNeuron8.setText(String.valueOf(networkOutput[8]));
		txtOutputNeuron9.setText(String.valueOf(networkOutput[9]));
	}

	public void setNetworkClassification(int output) {
		// TODO: James Set network classification through this function
		txtOutputClass.setText(String.valueOf(output));
	}

	// @Override
	public void actionPerformed(ActionEvent evt) {
		boolean windowsSystem = false;
		
		if(System.getProperty("os.name").toUpperCase().contains("WINDOWS"))
		{
			windowsSystem = true;
			
		} 
		if (Debug.DEBUG) System.out.println("Detected Operating System: " + System.getProperty("os.name"));
		
		if (evt.getSource() instanceof JButton)
		{ 
			// Button has been clicked
			if(evt.getSource() == btnSaveGraphic)
			{
				System.out.println("Desired == " + txtDesired.getText().toString());
				NeuralNetworkSet nnDataSet = new NeuralNetworkSet(sp.getOutput(),Integer.parseInt(txtDesired.getText().toString()));
			

				String wd = getDefaultDir();
				JFileChooser fc = new JFileChooser(wd);
				
				int rc = fc.showDialog(null, "Select Folder");
				
				if (rc == JFileChooser.APPROVE_OPTION)
				{
					
					File file = fc.getSelectedFile();
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					
					String filename = file.getAbsolutePath();
					setDefaultDir(filename);
					Filesystem.writeData(filename+"-"+nnDataSet.getDesired(), nnDataSet);
					
				}
				else
				{
					System.out.println("File chooser cancel button clicked");
					
				}
			} else if(evt.getSource() == btnLoadGraphic){
				NeuralNetworkSet nnDataSet = new NeuralNetworkSet(null,0);
				
					//sp.clearOutputArray();
					String wd = getDefaultDir();
					JFileChooser fc = new JFileChooser(wd);
					int rc = fc.showDialog(null, "Select Data File");
					if (rc == JFileChooser.APPROVE_OPTION)
					{
						File file = fc.getSelectedFile();
						String filename = file.getAbsolutePath();
						setDefaultDir(filename);
						nnDataSet = Filesystem.readData(filename);	// read data from file to NeuralNetworkSet Data Structure
						txtDesired.setText(String.valueOf(nnDataSet.getDesired())); // set desired text field 
						//sp.clearOutputArray();	// clear graphics pad	
						sp.setOutput(nnDataSet.getInput()); // draw image in file to graphics pad
						setStatus(file.getAbsolutePath());
					}
					else
					{
						System.out.println("File chooser cancel button clicked");
						
					}

				System.out.println();

			} else if (evt.getSource() == btnClassifyInput) {
				//we need to translate from the input being an array of array of ints, into a single array of double
				int[][] output = sp.getOutput();
				double[] input = new double[output.length * output[0].length + 1];
				int i = 0;
				for (int[] out : output) {
					for (int o : out) {
						input[i] = (double) o;
						i++;
					}
				}
				input[input.length-1] = -1;
				double[][] inputForNet = new double[1][input.length];
				inputForNet[0] = input;
				neuralNet.trainInputs = inputForNet;
				neuralNet.patNum = 0;
				neuralNet.numPatterns = 1;
				neuralNet.calcNet();
				this.setNetworkClassification((int) Math.round(neuralNet.outPred));
			} else if (evt.getSource() == btnSaveWeights) {
				//Not written
			} else if (evt.getSource() == btnLoadWeights) {
				String wd = getDefaultDir();
				JFileChooser fc = new JFileChooser(wd);
				int rc = fc.showDialog(null, "Select Data File");
				if (rc == JFileChooser.APPROVE_OPTION)
				{
					File file = fc.getSelectedFile();
					String filename = file.getAbsolutePath();
					setDefaultDir(filename);
					//Not written yet
					//neuralNet = ContinuousNeuronLayerClassifier.loadWeights(filename);
				}
				else
				{
					System.out.println("File chooser cancel button clicked");
					
				}
			} else if (evt.getSource() == btnRandomNet) {
				neuralNet = new NeuralNetwork();
			} else if (evt.getSource() == btnTrainTillValid) {
				//Implemented in the neural network when first loaded
			} else if(evt.getSource() == btnClearGraphic){
				 sp.repaint();
				 sp.clearOutputArray();
				 
			} else if(evt.getSource() == btnAddRandomNoise)
			{
				System.out.println("Add Random Noise");

				Vector<String> DirectoryList = Filesystem.getDirectoryFilesContaining("train", new File(System.getProperty("user.dir") + "\\src\\samples\\"));
				sp.addRandomNoise();
				for(int i=0; i<DirectoryList.size();i++)
				{
					NeuralNetworkSet nnDataSet = new NeuralNetworkSet(null,0);
					sp.clearOutputArray();
					nnDataSet = Filesystem.readData(DirectoryList.get(i));	// read data from file to NeuralNetworkSet Data Structure
					txtDesired.setText(String.valueOf(nnDataSet.getDesired())); // set desired text field 
					sp.setOutput(nnDataSet.getInput()); // draw image in file to graphics pad
					setStatus(DirectoryList.get(i).toString());
					sp.addRandomNoise();
					//System.out.println(System.getProperty("user.dir")+"\\src\\noise_samples\\train-"+nnDataSet.getDesired() + "\n" + DirectoryList.get(i).replace("samples", "noise_samples"));
					Filesystem.writeData(DirectoryList.get(i).replace("samples", "noise_samples").toString(), nnDataSet);
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else if(evt.getSource() == btnViewTrainingSamples)
			{
				
				String findString = "train";
				if (Integer.valueOf(txtDesired.getText()) <10 && Integer.valueOf(txtDesired.getText()) >= 0) {
					findString += "-" + Integer.valueOf(txtDesired.getText());
				} 
				Vector<String> DirectoryList = Filesystem.getDirectoryFilesContaining(findString, new File(System.getProperty("user.dir") + "\\src\\samples\\"));
				//sp.addRandomNoise();
				for(int i=0; i<DirectoryList.size();i++)
				{
					NeuralNetworkSet nnDataSet = new NeuralNetworkSet(null,0);
					sp.clearOutputArray();
					nnDataSet = Filesystem.readData(DirectoryList.get(i));	// read data from file to NeuralNetworkSet Data Structure
					txtDesired.setText(String.valueOf(nnDataSet.getDesired())); // set desired text field 
					sp.setOutput(nnDataSet.getInput()); // draw image in file to graphics pad
					setStatus(DirectoryList.get(i).toString());
					
					
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		
	}
	private String getDefaultDir()
	{
		String directory = null;
		
		if(this.defaultDir == null)
		{
			directory = System.getenv("user.dir");
		} else {
			directory = defaultDir;
		}
		System.out.println("gDefaultDir:\t"+ this.defaultDir);
		return directory;
	}
	
	private void setDefaultDir(String absDirectoryPath)
	{
		System.out.println("sDefaultDir:\t"+absDirectoryPath.subSequence(0, absDirectoryPath.lastIndexOf("\\")).toString());
		
		defaultDir = absDirectoryPath.subSequence(0, absDirectoryPath.lastIndexOf("\\")).toString();
	}
	

}
